#encoding:utf8
from handler import pathhandle
from handler import dbhandle
from plugin.oss_ctrl import OssCtrl

from funcs import get_dbcolt_list

from inc import dbmgr
from inc import dlmgr

def get_vultr_booksclt():
	
	db = dbmgr.DbMgr(host = "45.32.39.52", port=27017)
	db.use("kankindle")
	colt = db.use_collection("books")
	return colt
# ==============================
# TEST-pathhandle
# ==============================
#批量上传资源到oss2服务器
def test_pathhandle():
	incoss = OssCtrl.shareoss("oss-cn-beijing.aliyuncs.com", "I3qKdJMQC8TiuTCy", "MX0mTN960SezGAroN4Ldh1VgxSbf9g")
	incbkt = OssCtrl.sharebkt("pylib")

	def upload_end():
		print ("upload end")
	pathhandle.batch_uposs(incbkt, "D:\\script_test", "script_test/", upload_end)

# 下载所有的kindlebook的书籍
def test_download():
	clt = get_vultr_booksclt()
	booklist = get_dbcolt_list(clt)
	dllist = []
	for url in booklist:
		dllist.append(url)
	print (len(dllist), '--->>>len of dllist')
	def downend():
		print ("download all end!")
	dlpool = dlmgr.DlMgr(dllist, downend)
	dlpool.DL_PATH = "/data/kbook/"
	dlpool.start()

# 移除所有数据的字段
def test_remove_onekey():
	clt = get_vultr_booksclt()
	booklist = get_dbcolt_list(clt)
	for book in booklist:
		clt.update(dict(name=book["name"]), {"$unset":dict(filesize="",fileformat="")})

if __name__ == "__main__":
	# dlmgr = dlmgr.DlMgr([])
	test_download()
	